local skel = fk.CreateSkill {
  name = "sj_zhoushuhuizhan__gaiguantieweishan",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable {
  ["sj_zhoushuhuizhan__gaiguantieweishan"] = "盖棺铁围山",
  [":sj_zhoushuhuizhan__gaiguantieweishan"] = "<b>限定技</b>，未有火焰伤害造成的轮次结束时，你可以为每名角色分配1点火焰伤害；本局游戏你每造成1点火焰伤害，便可以转移其中1点。",

  ["@sj_zhoushuhuizhan__gaiguantieweishan"] = "盖棺铁围山",
  ["#sj_zhoushuhuizhan__gaiguantieweishan"] = "盖棺铁围山：你可以对所有角色造成1点火焰伤害",
  ["#sj_zhoushuhuizhan__gaiguantieweishan-ask"] = "盖棺铁围山：你可以对所有角色造成1点火焰伤害，且你可以转移其中 %arg 点伤害",

  ["click to exchange one damage"] = "点击转移<br>一点伤害",
}

skel:addEffect(fk.RoundEnd, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 then
      return #player.room.logic:getEventsOfScope(GameEvent.Damage,1,function (e)
        local damage = e.data
        return damage.damageType == fk.FireDamage and not damage.prevented
      end,Player.HistoryRound) == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local availablePlayerIds = table.map(table.filter(room:getAlivePlayers(), function(p)
      return p.rest > 0 or not p.dead
    end), Util.IdMapper)
    local num = math.min(#availablePlayerIds, player:getMark("@sj_zhoushuhuizhan__gaiguantieweishan"))
    if num == 0 then
      if room:askToSkillInvoke(player,{skill_name = skel.name, prompt = "#sj_zhoushuhuizhan__gaiguantieweishan"}) then
        event:setCostData(self, {
          change = true,
          tos = table.map(availablePlayerIds, function(id)
            return room:getPlayerById(id)
          end)
        })
        return true
      end
      return
    end
    local result = room:askToCustomDialog(player, {
      skill_name = skel.name,
      qml_path = "packages/sanshe/qml/GaiGuanTieWeiShanBox.qml",
      extra_data = {
        availablePlayerIds,
        num,
        "#sj_zhoushuhuizhan__gaiguantieweishan-ask::: " .. num,
      },
    })
    if result ~= "" then
      local tos, damage = {}, {}
      for _, v in ipairs(result) do
        if v[2] > 0 then
          table.insert(tos, room:getPlayerById(v[1]))
          table.insert(damage, v[2])
        end
      end
      event:setCostData(self, { tos = tos, damage = damage })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    local damage = cost_data.damage
    for i, to in ipairs(cost_data.tos) do
      if to and not to.dead then
        room:damage {
          from = player,
          to = to,
          damage = cost_data.change and 1 or damage[i],
          damageType = fk.FireDamage,
          skillName = skel.name,
        }
      end
    end
  end,
})

skel:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player == target and data.damageType == fk.FireDamage
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@sj_zhoushuhuizhan__gaiguantieweishan", data.damage)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local n = 0
  player.room.logic:getEventsOfScope(GameEvent.Damage,999,function (e)
    local damage = e.data
    if damage.from == player and damage.damageType == fk.FireDamage and not damage.prevented then
      n = n + damage.damage
      return true
    end
  end,Player.HistoryGame)
  player.room:setPlayerMark(player, "@sj_zhoushuhuizhan__gaiguantieweishan", n)
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@sj_zhoushuhuizhan__gaiguantieweishan", 0)
end)

return skel